# Create your views here.
#coding=utf-8
###django version 1.0
from django.conf.urls.defaults import *
from django.template import Context, loader
from django.http import HttpResponse
from lvproject.jj97.models import *
from django.views.decorators.cache import cache_page
from lvproject.jj97.common.RestView import RestView
from django.shortcuts import render_to_response, get_object_or_404
from lvproject import settings
import logging
import logging.config

logging.config.fileConfig(settings.APP_LOGGING_CONFIG_PATH)
#logging.basicConfig(level=settings.APP_LOGGING_LEVEL,
#                    format=settings.APP_LOGGING_FORMAT,
#                    filename=settings.APP_LOGGING_FILE_PATH,
#                    filemode=settings.APP_LOGGING_FILEMODE)
viewLogging = logging.getLogger('lvproject.jj97.views');

@cache_page(60*15)
def sitehome(request):
    t = loader.get_template('jj97/index.html');
    popularnews_list = SiteNews.objects.filter(sitename='http://www.ifensi.com/').order_by('newsdate')[:3];
    sportsnews_list = SiteNews.objects.filter(sitename='http://www.xinhuanet.com/sports/').order_by('newsdate')[:6];
    superstarnews_list = SiteNews.objects.filter(sitename='http://www.ifensi.com/').order_by('newsdate')[:6];
    economynews_list = SiteNews.objects.filter(sitename='http://www.china-cbn.com/').order_by('newsdate')[:6];
    centernews_list = SiteNews.objects.all().order_by('newsdate')[:8];
    
    c = Context({'popularnews_list':popularnews_list,
    'sportsnews_list':sportsnews_list,
    'superstarnews_list':superstarnews_list,
    'centernews_list':centernews_list,
    'economynews_list':economynews_list,});
    
    viewLogging.info("sitehome");
    
    return HttpResponse(t.render(c))


def newsdetail(request,newsid):
    #t = loader.get_template('jj97/newsdetail.html');
    newsobject = get_object_or_404(SiteNews,pk=newsid);
    
    viewLogging.info("newsdetail");
    #print newsobject.id;
    HTTPMethod = request.method.upper();
    if(HTTPMethod=='GET'):
        return render_to_response('jj97/newsdetail.html', {'newsobject': newsobject})
        #c=Context({'newsobject':newsobject,});
        #return HttpResponse(t.render(c));
    else:
        return None;
    
def inlinegoogle(request):
    t = loader.get_template('jj97/inlinegoogle.html');
    
    c=Context();
    
    return HttpResponse(t.render(c));

class NewsDetail(RestView):
    def GET(self,request,newsid):
        newsobject = get_object_or_404(SiteNews,pk=newsid);
        viewLogging.info("NewsDetail");
        return render_to_response('jj97/newsdetail.html', {'newsobject': newsobject})
